package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.WOrder;
import com.ruoyi.system.domain.WOrderDeliver;
import com.ruoyi.system.domain.vo.WOrderDeliverVO;
import com.ruoyi.system.mapper.WOrderDeliverMapper;
import com.ruoyi.system.mapper.WOrderMapper;
import com.ruoyi.system.service.IWOrderDeliverService;

/**
 * 物料订单发货Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-05
 */
@Service
public class WOrderDeliverServiceImpl implements IWOrderDeliverService 
{
    @Autowired
    private WOrderDeliverMapper wOrderDeliverMapper;
    @Autowired
    private WOrderMapper wOrderMapper;

    
    
    @Transactional
    @Override
    public R addDeliver(WOrderDeliver wOrderDeliver) {

    	String expressNo = wOrderDeliver.getExpressNo();
    	if (StringUtils.isEmpty(expressNo))  return R.error("没传expressNO");
    	Long goodNumber = wOrderDeliver.getGoodNumber();
    	if (goodNumber == null || goodNumber.longValue() <=0)  return R.error("没传goodNumber");
    	Long badNumber = wOrderDeliver.getBadNumber();
    	if (badNumber == null || badNumber.longValue() < 0)  return R.error("没传badNumber");
     	Long orderId = wOrderDeliver.getOrderId();
     	if (orderId == null || orderId.longValue() < 0)  return R.error("没传orderId");
     	WOrder selectWOrderById = wOrderMapper.selectWOrderById(orderId);
     	if (selectWOrderById.getOrderStatus() == 4) {
     		  return R.error("订单已经完成 ，不能发货");
		}
     	if (selectWOrderById.getOrderStatus() == 5) {
     		return R.error("订单已经取消 ，不能发货");
     	}
     	if (selectWOrderById.getOrderStatus() < 3) {
     		return R.error("订单还没生产 ，不能发货");
     	}
     	wOrderDeliver.setCustomerId(selectWOrderById.getCustomerId());
     	wOrderDeliver.setCustomerName(selectWOrderById.getCustomerName());
     	wOrderDeliver.setDeptId(selectWOrderById.getDeptId());
     	wOrderDeliver.setDeptName(selectWOrderById.getDeptName());
     	
     	
     	Long badNumberOrder = selectWOrderById.getBadNumber() == null ? 0 : selectWOrderById.getBadNumber() + badNumber;
     	Long goodNumberOrder = selectWOrderById.getGoodNumber() == null ? 0 : selectWOrderById.getGoodNumber() + goodNumber;
     	Long productNumber = selectWOrderById.getProductNumber();
     	if (productNumber <= (goodNumberOrder+badNumberOrder)) {
     		selectWOrderById.setOrderStatus(4);
		}
     	selectWOrderById.setBadNumber(badNumberOrder);
     	selectWOrderById.setGoodNumber(goodNumberOrder);
     	
     	wOrderMapper.updateWOrder(selectWOrderById);
     	wOrderDeliver.setCreateTime(new Date());
     	wOrderDeliverMapper.insertWOrderDeliver(wOrderDeliver);
     	
     	
    	return R.success();
    }
    
    /**
     * 查询物料订单发货
     * 
     * @param id 物料订单发货主键
     * @return 物料订单发货
     */
    @Override
    public WOrderDeliver selectWOrderDeliverById(Long id)
    {
        return wOrderDeliverMapper.selectWOrderDeliverById(id);
    }

    /**
     * 查询物料订单发货列表
     * 
     * @param wOrderDeliver 物料订单发货
     * @return 物料订单发货
     */
    @Override
    public List<WOrderDeliverVO> selectWOrderDeliverList(WOrderDeliverVO wOrderDeliverVO)
    {
        return wOrderDeliverMapper.selectWOrderDeliverListNew(wOrderDeliverVO);
    }

    @Override
    public WOrderDeliverVO selectWOrderDeliverInfo(WOrderDeliverVO wOrderDeliverVO) {
    	WOrderDeliverVO selectWOrderDeliverByIdNew = wOrderDeliverMapper.selectWOrderDeliverByIdNew(wOrderDeliverVO.getId());
    	
    	
    	return selectWOrderDeliverByIdNew;
    }
    
    /**
     * 新增物料订单发货
     * 
     * @param wOrderDeliver 物料订单发货
     * @return 结果
     */
    @Override
    public int insertWOrderDeliver(WOrderDeliver wOrderDeliver)
    {
        wOrderDeliver.setCreateTime(DateUtils.getNowDate());
        return wOrderDeliverMapper.insertWOrderDeliver(wOrderDeliver);
    }

    /**
     * 修改物料订单发货
     * 
     * @param wOrderDeliver 物料订单发货
     * @return 结果
     */
    @Override
    public int updateWOrderDeliver(WOrderDeliver wOrderDeliver)
    {
        wOrderDeliver.setUpdateTime(DateUtils.getNowDate());
        return wOrderDeliverMapper.updateWOrderDeliver(wOrderDeliver);
    }

    /**
     * 批量删除物料订单发货
     * 
     * @param ids 需要删除的物料订单发货主键
     * @return 结果
     */
    @Override
    public int deleteWOrderDeliverByIds(Long[] ids)
    {
        return wOrderDeliverMapper.deleteWOrderDeliverByIds(ids);
    }

    /**
     * 删除物料订单发货信息
     * 
     * @param id 物料订单发货主键
     * @return 结果
     */
    @Override
    public int deleteWOrderDeliverById(Long id)
    {
        return wOrderDeliverMapper.deleteWOrderDeliverById(id);
    }
}
